﻿using System.ComponentModel;
using System.Diagnostics;
using Wintellect.Sterling;
using Wintellect.Sterling.Database;

namespace Wulong.Phone.Utils
{
    public class SterlingService<T> : ISterlingInstance where T : BaseDatabaseInstance
    {
        private SterlingEngine _engine;

        public ISterlingDatabaseInstance Database { get; private set; }

        private SterlingDefaultLogger _logger;

        public void Activate()
        {
            if (DesignerProperties.IsInDesignTool)
                return;

            if (Debugger.IsAttached)
            {
                _logger = new SterlingDefaultLogger(SterlingLogLevel.Verbose);
            }

            _engine = new SterlingEngine();
            _engine.Activate();

            Database = _engine.SterlingDatabase.RegisterDatabase<T>();

            //TRIGGERS HERE ETC...
        }

        public void Deactivate()
        {
            if (Debugger.IsAttached && _logger != null)
                _logger.Detach();

            _engine.Dispose();

            Database = null;
            _engine = null;
        }

    }


}
